<template>
    <!-- $store.state.isOpenLogin -->
    <div class="bg" v-show="$store.state.isOpenLogin">
        <div class="modal" v-if="loginnum===0">
            <div class="title">
                登录
                <span @click="clickFn">X</span>
            </div>
            <div class="box">
                <div class="box_l">
                    <img src="../assets/shouji.png" alt="">
                </div>
                <div class="box_r">
                    <div class="sm">
                        <h3>扫码登录</h3>
                        <img :src="qrimg" alt="">
                    </div>
                    <p>使用<span>网易云音乐APP </span>扫码登录</p>

                </div>
            </div>
            <div class="btn" @click="loginnum=1">其他登录方式</div>
        </div>
        <div class="modal2" v-else-if="loginnum===1">
            <div class="title">
                登录
                <span @click="clickFn">X</span>
            </div>
            <div class="box">
                <div class="box_l">
                    <img src="../assets/diannao.png" alt="">
                    <div class="shouji" @click="loginnum=2">手机号登录</div>
                    <div class="zc" @click="loginnum=3">注册</div>
                </div>
                <div class="box_r">
                    <ul>
                        <li><span></span><i>微信登录</i></li>
                        <li><span></span><i>QQ登录</i></li>
                        <li><span></span><i>微博登录</i></li>
                        <li><span></span><i>网易云邮箱账号登录</i></li>
                    </ul>
                </div>
            </div>
            <div class="footer">
                <p>同意<span>《服务条款》</span> <span>《隐私政策》</span> <span>《儿童隐私政策》</span></p>
                <div>
                    <img :src="qrimg" alt="">
                </div>
            </div>
        </div>
        <div class="modal3" v-else-if="loginnum===2">
            <div class="title">
                手机号登录
                <span @click="clickFn">X</span>
            </div>

            <div class="neirong">
                <div class="pohen">
                    <div>
                        <em>+86</em>
                        <select>
                            <option value="" selected></option>
                            <option value="">中国+86</option>
                        </select>
                    </div>
                    <input type="text" v-model="pohenVal">
                </div>
                <!-- 验证码滑块(拼图) -->

                <div class="verify_box">
                    <slide-verify :l="42" :r="10" :w="360" :h="155" slider-text="向右滑动" @success="onSuccess"
                        @fail="onFail" @refresh="onRefresh"></slide-verify>
                    <div>{{ msg }}</div>
                </div>
                <div class="yzm">
                    <input type="text" placeholder="请输入验证码" v-model="smsNum">
                    <div @click="getSmsNum">
                        <span v-if="isShowNum">获取验证码</span>
                        <span v-else class="daojis">{{ num }}s</span>
                    </div>

                </div>
                <div class="login" @click="LoginFn">登录</div>
                <span class="mima">密码登录</span>
            </div>
            <div class="footer">
                <span class="denlu" @click="loginnum=1">&lt;其他登录方式</span>
                <span class="zc" @click="loginnum=3">&lt;没有账号?免费注册</span>
            </div>
        </div>
        <div class="modal4" v-else>
            <div class="title">
                手机注册
                <span @click="clickFn">X</span>
            </div>
            <div>
                <input type="text">
                <input type="text">
                <div></div>
                <div>
                    <span></span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { erweima, scerweima, yanzheng, dlzhuangtai ,yanzhengma,yzyanzhengma,sx,shoujidenglu} from "@/request/api"
export default {
    data() {
        return {
            msg: "",
            key: '',
            qrurl: '',
            qrimg: '',
            isLogin: false,
            pohenVal: '',
            qrCheckData: {},//状态,
            num: 60,
            isShowNum: true,
            smsNum: '',
            loginnum:0

        }
    },
    mounted() {
        this.getKey()
    },
    methods: {
        onSuccess() {
            this.msg = "验证成功";
        },
        onFail() {
            this.msg = "验证失败";
        },
        onRefresh() {
            this.msg = "再来一次";
        },
        getSmsNum() {
            // 验证手机号
            let ret = /^(13[0-9]|14[01456789]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            // if(正则对象.test(用户输入信息))
            if (!ret.test(this.pohenVal.trim())) {
                let toastObj = {
                    content: "请输入正确的手机号",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
                return
            }
            // 滑块验证
            if (this.msg != "验证成功") {
                let toastObj = {
                    content: "请将滑块拖到正确的位置",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
                return
            }

            yanzhengma(this.pohenVal).then(res =>{
                console.log(res);
            if (res.data.code === 200) {
                let toastObj = {
                    content: "获取验证码成功",
                    icon: "success",
                };
                this.$store.dispatch('AsncToast', toastObj)
                // 改变按钮状态
                this.isShowNum = false
                // 倒计时
                let iterm = setInterval(() => {
                    this.num--
                    if (this.num === 0) {
                        // ifnum=0清除倒计时并改变显示的按钮为获取验证码,并给num重新赋值
                        clearInterval(iterm)
                        this.isShowNum = true
                        this.num = 60
                    }
                }, 1000);
            } else {
                let toastObj = {
                    content: "小时发送受限,请一小时后在发送",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
            }
            })
        },
        LoginFn(){
            if (this.phoneNum === '') {
                let toastObj = {
                    content: "手机号不能为空",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
                return
            }
            if (this.smsNum === '') {
                let toastObj = {
                    content: "验证码不能为空",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
                return
            }
            if (this.msg != "验证成功") {
                let toastObj = {
                    content: "请将滑块拖到正确的位置",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
                return
            }
            yzyanzhengma({
                phone:this.pohenVal,captcha:this.smsNum
            }).then(res =>{
                // console.log(res);
                if(res.data.data === true){
                    shoujidenglu({
                        phone:this.pohenVal,captcha:this.smsNum
                    }).then(res =>{
                        console.log(res);
                        sessionStorage.setItem('cookie', res.data.cookie);
                        this.getStatus()
                    })
                    this.$store.commit('changeIsLogin', false)
                }else{
                    let toastObj = {
                    content: "请输入正确的验证码",
                    icon: "danger",
                };
                this.$store.dispatch('AsncToast', toastObj)
                }
            })
        },
        getKey() {
            erweima({
                timerstamp: new Date().getTime(),
            }).then(res => {
                // console.log(res);
                this.key = res.data.data.unikey
            })
            this.loginQqr(this.key)
        },
        loginQqr(key) {
            scerweima({ key: key, timerstamp: new Date().getTime(), qrimg: true }).then(res => {
                // console.log(res);
                this.qrurl = res.data.data.qrurl
                this.qrimg = res.data.data.qrimg
                this.qrCheck()
            })
        },
        qrCheck() {
            let qrCheckData = yanzheng({
                key: this.key,
                timerstamp: new Date().getTime(),//传入参数时间戳
                withCredentials: true
            }).then(res => {
                // console.log(res);
                this.qrCheckData = res.data
                sessionStorage.setItem('cookie', res.data.cookie);
                // this.isLogin = true
            })
        },
        getStatus() {
            dlzhuangtai(sessionStorage.getItem("cookie")).then(res => {
                sessionStorage.getItem("cookie");
                // console.log(res.data.data.account.id,res.data.data.account.status);
                // console.log(res.data.data.profile.avatarUrl,res.data.data.profile.nickname);

                sessionStorage.setItem('isLogin', res.data.data.account.status);
                sessionStorage.setItem('userid', res.data.data.account.id);
                sessionStorage.setItem('avatarUrl', res.data.data.profile.avatarUrl);
                sessionStorage.setItem('nickname', res.data.data.profile.nickname);
                console.log(res);

                // console.log( sessionStorage.getItem('isLogin'));
                // console.log(sessionStorage.getItem("nickname"));
            })
        },
        clickFn() {
            // console.log(1);
            this.$store.commit('changeIsLogin', false)
        }
    },
    created() {

    },
    watch: {
        isLogin: function () {
            let times = setInterval(async () => {
                this.qrCheck()
                let code = this.qrCheckData.code
                // 判断返回值中的code
                if (code === 801) {

                } else if (code === 802) {

                } else if (code === 803) {

                    sessionStorage.setItem("user", true);


                    this.$router.push("/");
                    //用户登录成功后清除定时器
                    clearInterval(times)
                    //获取登录之后的uid和用户详情以及登录状态
                    this.getStatus()
                    // this.getUinfo()


                }
            }, 5000)
        }
    }
}
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";

.bg {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 19;
    background-color: rgba(0, 0, 0, 0.5);

    .modal {
        background-color: #fff;
        width: 530px;
        position: absolute;
        left: 0;
        top: 25%;
        right: 0;
        margin: auto;

        .title {
            width: 100%;
            height: 40px;
            font-size: 14px;
            padding: 0px 20px;
            box-sizing: border-box;
            line-height: 40px;
            color: #fff;
            background-color: #000;
            font-weight: bold;

            span {
                float: right;
                font-weight: 400;
            }
        }

        .box {
            margin: 20px 0;
            display: flex;
            justify-content: center;
            align-items: center;

            .box_l {
                img {
                    width: 125px;
                    height: 220px;
                }
            }

            .box_r {
                p {
                    margin-left: 55px;
                    text-align: center;
                    font-size: 12px;
                    line-height: 17px;
                    color: rgba(0, 0, 0, 0.4);

                    span {
                        cursor: pointer;
                        color: #0c73c2;
                    }
                }

                .sm {
                    position: relative;
                    margin-left: 60px;

                    span {
                        position: absolute;
                        left: 0;
                        top: 0;
                    }

                    h3 {
                        font-size: 18px;
                        color: #333;
                        width: 150px;
                        text-align: center;
                    }
                }
            }
        }

        .btn {
            display: block;
            width: 118px;
            height: 28px;
            margin: 20px auto 0;
            padding-right: 0;
            font-size: 12px;
            border: 1px solid #979797;
            border-radius: 15px;
            line-height: 28px;
            text-align: center;
            color: rgba(0, 0, 0, 0.8);
            margin-bottom: 20px;
            cursor: pointer;
        }
    }

    .modal2 {
        background-color: #fff;
        width: 530px;
        position: absolute;
        left: 0;
        top: 25%;
        right: 0;
        margin: auto;

        .title {
            width: 100%;
            height: 40px;
            font-size: 14px;
            padding: 0px 20px;
            box-sizing: border-box;
            line-height: 40px;
            color: #fff;
            background-color: #000;
            font-weight: bold;

            span {
                float: right;
                font-weight: 400;
            }
        }

        .box {
            transform: translateY(20px);
            margin: 20px 0;
            display: flex;
            justify-content: center;
            align-items: center;
            box-sizing: border-box;

            .box_l {
                padding-right: 30px;
                border-right: 1px dotted #ccc;

                img {
                    width: 224px;
                    height: 120px;
                }

                .shouji {
                    height: 32px;
                    background-color: #0c73c2;
                    padding: 0 15px 0 20px;
                    height: 100%;
                    line-height: 31px;
                    text-align: center;
                    border-radius: 5px;
                    font-size: 12px;
                    color: #fff;
                    margin-bottom: 18px;
                    cursor: pointer;
                }

                .zc {
                    padding: 0 15px 0 20px;
                    height: 100%;
                    line-height: 31px;
                    text-align: center;
                    background-color: #f5f5f5;
                    border: 1px solid #ccc;
                    border-radius: 5px;
                    cursor: pointer;
                }
            }

            .box_r {
                padding-left: 30px;
                height: 218px;

                ul {
                    li {
                        text-align: center;
                        width: 148px;
                        height: 39px;
                        margin-bottom: 22px;
                        font-size: 12px;
                        color: #333;
                        line-height: 39px;

                        span {
                            margin-top: 5px;
                            margin-right: 10px;
                            display: inline-block;
                            width: 38px;
                            height: 38px;

                            &:nth-child(1) {
                                background: url(../assets/denlu.png) no-repeat;
                                background-size: 314px 414px;
                                background-position: -152px -308px;
                            }

                            &:nth-of-type(2) {
                                background: url(../assets/denlu.png) no-repeat -152px -308px;
                                background-size: 314px 414px;
                            }

                            &:nth-of-type(3) {
                                background: url(../assets/denlu.png) no-repeat;
                                background-size: 314px 414px;
                                background-position: -152px -308px;
                            }

                            &:nth-of-type(4) {
                                background: url(../assets/denlu.png) no-repeat;
                                background-size: 314px 414px;
                                background-position: -152px -308px;
                            }
                        }

                    }

                }
            }
        }

        .footer {
            padding: 0px 20px;
            padding-top: 30px;
            box-sizing: border-box;

            p {
                font-size: 12px;

                span {
                    color: #0c73c2;
                    cursor: pointer;
                }
            }

            div {
                position: relative;
                float: right;
                width: 52px;
                height: 52px;
                overflow: hidden;
                margin-bottom: 20px;

                img {

                    position: absolute;
                    width: 52px;
                    height: 52px;
                    top: 18px;
                    left: 18px;
                }
            }

        }
    }

    .modal3 {
        background-color: #fff;
        width: 530px;
        position: absolute;
        left: 0;
        top: 25%;
        right: 0;
        margin: auto;

        .title {
            width: 100%;
            height: 40px;
            font-size: 14px;
            padding: 0px 20px;
            box-sizing: border-box;
            line-height: 40px;
            color: #fff;
            background-color: #000;
            font-weight: bold;

            span {
                float: right;
                font-weight: 400;
            }
        }

        .neirong {
            padding-top: 80px;

            .verify_box {
                width: 360px;
                margin: 0 auto 20px;

                /deep/.slide-verify {
                    width: 100% !important;

                    canvas {
                        position: absolute;
                        top: -150px;
                        left: 0;
                        display: none;
                        box-sizing: border-box;
                    }

                    &:hover {
                        canvas {
                            display: block;
                        }
                    }
                }
            }

            .pohen {
                width: 320px;
                height: 36px;
                border: 1px solid #ccc;
                border-radius: 18px;
                margin: auto;
                position: relative;
                display: flex;

                div {
                    width: 54px;
                    height: 36px;
                    text-align: center;
                    line-height: 36px;

                    em {
                        font-size: 12px;
                        color: #666;
                    }

                    select {
                        width: 18px;
                        position: absolute;
                        top: 9px;
                        left: 42px;
                        option {
                            
                            font-size: 12px;
                            color: #666;
                        }
                    }
                }

                input {
                    margin-left: 10px;
                    border: 0;
                    outline: none;
                }
            }

            .yzm {
                width: 320px;
                margin: auto;
                display: flex;
                justify-content: space-between;
                margin-top: 20px;

                input {
                    width: 190px;
                    height: 36px;
                    line-height: 36px;
                    padding-left: 15px;
                    outline: none;
                    border: 1px solid #ccc;
                    border-radius: 18px;
                    font-size: 14px;
                    color: #ccc;

                }

                div {
                    span {
                        display: inline-block;
                        width: 119px;
                        height: 36px;
                        margin-left: 15px;
                        border-radius: 18px;
                        line-height: 36px;
                        text-align: center;
                        font-size: 14px;
                        color: #fff;
                        background-color: #ff3a3a;
                        cursor: pointer;
                    }
                }
            }

            .login {
                width: 320px;
                height: 36px;
                margin: auto;
                margin-top: 20px;
                margin-bottom: 50px;
                border-radius: 18px;
                background-color: #ff3a3a;
                line-height: 36px;
                text-align: center;
                font-size: 14px;
                color: #fff;
                cursor: pointer;
            }

            .mima {
                font-size: 12px;
                color: #666;
                margin-left: 106px;
                cursor: pointer;
            }
        }

        .footer {
            width: 100%;
            height: 49px;
            border-top: 1px solid #ccc;
            box-sizing: border-box;
            background-color: #f7f7f7;
            line-height: 49px;
            text-align: center;
            padding: 0 20px;
            margin-top: 20px;
            display: flex;
            justify-content: space-around;

            .denlu {
                font-size: 12px;
                color: #0c73c2;
                cursor: pointer;
            }

            .zc {
                font-size: 12px;
                color: #999;
                cursor: pointer;
            }
        }
    }

    .modal4 {
        background-color: #fff;
        width: 530px;
        position: absolute;
        left: 0;
        top: 25%;
        right: 0;
        margin: auto;

        .title {
            width: 100%;
            height: 40px;
            font-size: 14px;
            padding: 0px 20px;
            box-sizing: border-box;
            line-height: 40px;
            color: #fff;
            background-color: #000;
            font-weight: bold;

            span {
                float: right;
                font-weight: 400;
            }
        }
    }
}
</style>